<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_category\Entity\ShusyCategory;
use Drupal\shusy_store\Entity\ShusyStore;
use Drupal\shusy_store_bind_level\Entity\ShusyStoreBindLevel;
use Drupal\shusy_store_category\Entity\ShusyStoreCategory;

/**
 * @OA\Schema(
 *   schema="StoreItem",
 *   type="object",
 *   description="StoreItem"
 * )
 */
class StoreItem extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_store';
  }

  protected function getDistance($lat1, $lng1, $lat2, $lng2) {
    //将角度转为狐度
    $radLat1 = deg2rad($lat1);//deg2rad()函数将角度转换为弧度
    $radLat2 = deg2rad($lat2);
    $radLng1 = deg2rad($lng1);
    $radLng2 = deg2rad($lng2);
    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137;
    return $s;
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyStore) {
      [$latitude, $longitude] = $this->context;
      $imgs = $this->getImgs($this->wrapper->field_images, 'imgswatermark', 'simgswatermark');
      //添加营业执照
      $business = file_load($this->entity->business);
      $imgs[0][] = $this->imageStyleUrl('imgswatermark', $business);
      $imgs[1][] = $this->imageStyleUrl('simgswatermark', $business);

      $value = [
        'distance' => $this->getDistance($latitude, $longitude, $this->entity->latitude, $this->entity->longitude),
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity->id,
        /**
         * @OA\Property(
         *   property="name",
         *   type="string",
         *   description="店铺名",
         * ),
         */
        'name' => (string) $this->entity->store_name,
        /**
         * @OA\Property(
         *   property="category",
         *   ref="#/components/schemas/CategoryItem",
         *   description="店铺分类 1级分类",
         * ),
         */
        'category' => $this->getCategory($this->entity),
        /**
         * @OA\Property(
         *   property="logo",
         *   type="string",
         *   description="LOGO url",
         * ),
         */
        'logo' => $this->imageStyleUrl('StoreItemLogo', file_load($this->entity->logo)),
        /**
         * @OA\Property(
         *   property="star",
         *   type="integer",
         *   description="店铺评星",
         * ),
         */
        'star' => shusy_store_star($this->entity->id),
        /**
         * @OA\Property(
         *   property="score",
         *   type="number",
         *   description="店铺评分",
         * ),
         */
        'score' => shusy_store_score($this->entity->id),
        /**
         * @OA\Property(
         *   property="open",
         *   type="string",
         *   description="营业时间",
         * ),
         */
        'open' => (string) $this->entity->open,
        /**
         * @OA\Property(
         *   property="tel",
         *   type="string",
         *   description="店铺电话",
         * ),
         */
        'tel' => (string) $this->entity->phone,
        /**
         * @OA\Property(
         *   property="address",
         *   type="string",
         *   description="店铺地址",
         * ),
         */
        'address' => (string) $this->entity->store_address,
        /**
         * @OA\Property(
         *   property="longitude",
         *   type="number",
         *   description="经度",
         * ),
         */
        'longitude' => (float) $this->entity->longitude,
        /**
         * @OA\Property(
         *   property="latitude",
         *   type="number",
         *   description="纬度",
         * ),
         */
        'latitude' => (float) $this->entity->latitude,
        /**
         * @OA\Property(
         *   property="sale_total",
         *   type="integer",
         *   description="总销量",
         * ),
         */
        'sale_total' => shusy_store_sale_total($this->entity->id),
        /**
         * @OA\Property(
         *   property="sale_month",
         *   type="integer",
         *   description="月销量",
         * ),
         */
        'sale_month' => shusy_store_sale_month($this->entity->id),
        /**
         * @OA\Property(
         *   property="level",
         *   ref="#/components/schemas/StoreLevel",
         *   description="店铺级别",
         * ),
         */
        'level' => $this->getLevel($this->entity),
        'permit' => !empty($imgs),
        'imgs' => $imgs,
      ];
      $this->merge($value);
    }
  }

  protected function getLevel(ShusyStore $store) {
    //计算店铺级别
    $es = shusy_store_bind_level_load_multiple(FALSE, [
      'store_id' => $store->id,
    ]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyStoreBindLevel && $e->status == 1) {
        return StoreLevel::init($e->level_id);
      }
    }
    return FALSE;
  }

  protected function getCategory(ShusyStore $shusyStore) {
    $es = shusy_store_category_load_multiple(FALSE, ['store_id' => $shusyStore->id]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyStoreCategory) {
        $category = shusy_category_load($e->category_id);
        if ($category instanceof ShusyCategory) {
          return CategoryItem::get(FALSE, FALSE, [
            $category->id,
            $category->name,
          ]);
        }
      }
    }
    return ['id' => 0, 'name' => ''];
  }

}
